package syn;

import java.util.ArrayList;


// 不安全集合类
public class TestUnsafeList {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        // 创建10w个线程
        for (int i = 0; i < 100000; i++) {
            new Thread(()->{
                // 同步代码块，加上同步监视器,同步监视器一般是共享资源
                synchronized (list){
                    list.add(Thread.currentThread().getName());
                }
            }).start();
        }
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(list.size());   //99994
    }
}
